#include "include/Preferences.h"
#include "ui_Preferences.h"

#include "include/Common.h"

#include <QSettings>
#include <QDebug>
#include <QAbstractButton>
#include <QMessageBox>

Preferences::Preferences(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::Preferences)
{
	ui->setupUi(this);

//	ui->w_convertPath->setPlaceholderText("Enter path to convert here");
	ui->w_convertPath->setText(     QSettings().value("convertPath").toString());

//	ui->w_latexPath->setPlaceholderText("Enter path to latex here");
	ui->w_latexPath->setText(       QSettings().value("latexPath").toString());

//	ui->w_tex2imPath->setPlaceholderText("Enter path to tex2im here");
	ui->w_tex2imPath->setText(      QSettings().value("tex2imPath").toString());

	int maxImageWidth = QSettings().value("maxImageWidth", 300).toInt();
	int maxImageHeight = QSettings().value("maxImageHeight", 300).toInt();

	ui->w_imageWidth->setValue(maxImageWidth);
	ui->w_imageHeight->setValue(maxImageHeight);
}

Preferences::~Preferences()
{
	delete ui;
}

void Preferences::on_buttonBox_accepted()
{
	ui->shortcutPreferencesWidget->applyChanges();
}

void Preferences::on_buttonBox_clicked(QAbstractButton* button)
{
	if (button->text() == "&OK") // yuk
	{
		QSettings().setValue("convertPath",     QVariant(ui->w_convertPath->text()));
		QSettings().setValue("latexPath",       QVariant(ui->w_latexPath->text()));
		QSettings().setValue("tex2imPath",      QVariant(ui->w_tex2imPath->text()));
		QSettings().setValue("maxImageWidth",   QVariant(ui->w_imageWidth->value()));
		QSettings().setValue("maxImageHeight",  QVariant(ui->w_imageHeight->value()));
	}
}

void Preferences::setShortcuts(MuCustomShortcuts* shortcuts)
{
	ui->shortcutPreferencesWidget->setShortcuts(shortcuts);
}

void Preferences::on_restoreFactorySettings_clicked()
{
	QString message = "This will restore all your settings, INCLUDING LICENSING\nAre you sure you know what you are doing?";

	const QString title = "Restore Factory Settings";

	int button = QMessageBox::warning(0,
									   title,
									   message, QMessageBox::Yes, QMessageBox::Cancel);

	if (button == QMessageBox::Yes)
	{
		QSettings().clear();
	}

	close();
}


//////////////////////////////////////


void DependencyDetector::operator()()
{
#if defined(__unix__) or defined(__APPLE_CC__)

	QString latex = sysExec("which latex");
	QString convert = sysExec("which convert");

	QString convertPref = QSettings().value("convertPath").toString();
	QString latexPref = QSettings().value("latexPath").toString();

	bool prevDialogShown = false;
	bool prevOKPressed = false;

	if ((convertPref == "" && convert != "") ||
	        (latexPref == "" && latex != ""))
	{
		prevDialogShown = true;
		QString message = "The following dependencies have been detected:\n\n";
		if (latex != "")
			message += "- latex\n";
		if (convert != "")
			message += "- convert\n\n";

		message += "should they be added to your preferences?";

		const QString title = "Dependencies detected";

		int button = QMessageBox::question(0,
		                                   title,
		                                   message, QMessageBox::No, QMessageBox::Yes);

		if (button == QMessageBox::Yes)
		{
			QSettings().setValue("convertPath",     QVariant(convert));
			QSettings().setValue("latexPath",       QVariant(latex));
			prevOKPressed = true;
		}
	}
	if (QSettings().value("tex2imPath").toString() == "" &&
	        ((prevDialogShown && prevOKPressed) || prevDialogShown == false))
	{
		QString message = "The tex2im script needs to be found so that latex\n\
						  can function properly. Open prefs now?";

		const QString title = "Missing dependency";

		int button = QMessageBox::question(0,
		                                   title,
		                                   message, QMessageBox::No, QMessageBox::Yes);

		if (button == QMessageBox::Yes)
		{
			Preferences* prefs = new Preferences(0);
			prefs->show();
		}
	}
#endif
}
